<?php
namespace app\admin\controller;

use app\api\controller\KbxOther;
use think\db;
use think\Lang;
use app\admin\model\SupplierAdmin;
use app\api\controller\Common as CommonApi;

class Supplier extends User{

    //列表
    public function index()
    {
        $post = request()->post();
        //获取供应商列表
        $KbxOther = new KbxOther();
        $supplierList = $KbxOther->kbx_suppliers_list();
        $where = [];
        if(!empty($post['keywords'])) $where['supplier_account|supplier_name'] = ['like','%'.$post['keywords'].'%'];
        if(!empty($post['company_id'])) $where['company_id'] = $post['company_id'];
        $list = db('supplier_admin')->where($where)->select();
        foreach ($list as $key=>$value){
            $list[$key]['company_info'] = [];
            foreach ($supplierList as $s_k=>$s_v){
                if($s_v['id']==$value['company_id']){

                    $list[$key]['company_info'] = $s_v;
                    break;
                }
            }
        }
        $this->assign('supplierList',$supplierList)
            ->assign('list',$list)
            ->assign('wherelist',$post);

        return $this->fetch();
    }

    //新增
    public function create()
    {
        //检查权限 非超管不可操作
        if(!$this->checkUpdateAdminAuth()) return $this->result([],0,Lang::get('无操作权限'),'json');
        if(request()->isPost()){
            //检查账号
            $supplier_admin_id = getCookieSupplierAdmin();
            $adminInfo = (new CommonApi())->getSupplierAdminInfo($supplier_admin_id);
            $post = request()->post();
            if(empty($post['supplier_name'])) return $this->error(Lang::get('缺少参数').'supplier_name');
            if(empty($post['supplier_account'])) return $this->error(Lang::get('缺少参数').'supplier_account');
            if(empty($post['supplier_pwd'])) return $this->error(Lang::get('缺少参数').'supplier_pwd');
            if(db('supplier_admin')->where('supplier_account',$post['supplier_account'])->find()){
                return $this->error(Lang::get('供应商ID已存在'));
            }
            $post['supplier_pwd'] = password($post['supplier_pwd']);
            $post['company_id'] = $adminInfo['company_id'];//相同供应商
            $post['is_super'] = 0;//普通管理员
            $post['admin_cate_id'] = 1;//权限
            $post['create_time'] = $post['update_time'] = time();
//            var_dump($post);exit;
            $db = new SupplierAdmin();
            $res = $db->allowField(true)->save($post);
            if($res!==false){
//                return $this->result([],200,Lang::get('创建成功'),'json');
                return $this->success(Lang::get('创建成功'));
            }else{
                return $this->error(Lang::get('创建失败'));
//                return $this->result([],0,Lang::get('创建失败'),'json');
            }
        }
        return $this->fetch();
    }

    //修改
    public function edit()
    {
        $id = input('id',0);
        $supplierInfo = (new CommonApi())->getSupplierAdminInfo($id);
        //检查权限 非超管不可操作
        if(request()->isPost()){
            //检查账号
            $post = request()->post();
            if(empty($post['supplier_admin_id'])) return $this->error(Lang::get('缺少参数').'supplier_admin_id');
            $supplier_admin_id = $post['supplier_admin_id'];
            //$supplierInfo = (new CommonApi())->getAdminInfo($supplier_admin_id);
            //超管不可禁用
            //if($supplierInfo['is_super']==1 && empty($post['is_use'])) return $this->error(Lang::get('超级管理员不可禁用'));
            unset($post['supplier_admin_id']);
            $res = db('supplier_admin')->where('supplier_admin_id',$supplier_admin_id)->update($post);
            if($res!==false){
                return $this->result([],200,Lang::get('修改成功'),'json');
            }else{
                return $this->result([],0,Lang::get('修改失败'),'json');
            }
        }

        $this->assign('supplierInfo',$supplierInfo);
        return $this->fetch();
    }

    //检查更新权限
    public function checkUpdateAdminAuth()
    {
        $supplier_id = getCookieSupplierAdmin();
        $supplier = (new CommonApi())->getSupplierAdminInfo($supplier_id);

        return $supplier['is_super']==1;
    }

    public function role()
    {
        return $this->result([],200,'正在开发','json');
    }
}